#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>

int main()
{
  int fds[2] = {0};
  if(pipe(fds) < 0){
    perror("pipe");
    exit(1);
  }

  pid_t fd = fork();
  if(fd < 0){
    perror("fork");
    exit(2);
  }

  if(fd == 0){
    close(fds[0]);
    const char* msg = "Hello father process!";
    //while(1)
    write(fds[1], msg, strlen(msg));
    exit(1);
  }

  close(fds[1]);
  waitpid(-1, NULL, 0);
  char msg[128];
  ssize_t s = read(fds[0], msg, sizeof(msg) - 1);
  if(s < 0){
    perror("read");
    exit(2);

  }
  else if(s == 0){
    printf("read end");
    exit(0);
  }
  else{
    msg[s] = 0;
    printf("child# %s\n", msg);
  }
  return 0;
}
